﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SmartSpace.Web
{
    public static class RequestUtility
    {
        public static Guid GetGuid(this HttpRequest request, string key)
        {
            string strValue = request.QueryString[key];
            if(string.IsNullOrEmpty(strValue))
            {
                return Guid.Empty;
            }

            try
            {
                return new Guid(strValue);
            }
            catch
            {
                return Guid.Empty;
            }
        }

        public static string GetString(this HttpRequest request, string key)
        {
            return request.QueryString[key];
        }

        public static int GetInt(this HttpRequest request, string key)
        {
            return GetInt(request, key, 0);
        }

        public static int GetInt(this HttpRequest request, string key, int nullValue)
        {
            string strValue = request.QueryString[key];
            if (string.IsNullOrEmpty(strValue))
            {
                return nullValue;
            }

            try
            {
                return Convert.ToInt32(strValue);
            }
            catch
            {
                return nullValue;
            }
        }
    }
}
